package com.undsf.despot.modles;

import java.util.List;

/**
 * Created by Arathi on 2016/10/3.
 */
public class Command implements Comparable<Command> {
    public static final int INSTRUCT_WAIT = 0;
    public static final int INSTRUCT_COMBAT = 1;
    public static final int INSTRUCT_ATTACK = 11;
    public static final int INSTRUCT_DEFENSE = 12;
    public static final int INSTRUCT_CAST = 13;
    public static final int INSTRUCT_ITEM = 14;
    public static final int INSTRUCT_RUN = 2;

    public static final int TARGET_TYPE_NONE = 0;
    public static final int TARGET_TYPE_SELF = 10;
    public static final int TARGET_TYPE_ALLY_SINGLE = 11;
    public static final int TARGET_TYPE_ALLY_ALL = 12;
    public static final int TARGET_TYPE_ENEMY_SINGLE = 21;
    public static final int TARGET_TYPE_ENEMY_TEAM = 22;
    public static final int TARGET_TYPE_ENEMY_ALL = 23;

    protected Unit src;
    protected List<Unit> targets;
    protected String action;
    protected int speed;

    protected int instruct;
    protected Skill skill;
    protected Item item;
    // protected boolean needTarget;
    protected int targetType;

    public Command() {}

    public Command(Unit u) {
        src = u;
        speed = u.speed;
    }

    public void setAction(String action) {
        this.action = action;
        if (action.equals("attack")) {
            instruct = INSTRUCT_ATTACK;
            targetType = TARGET_TYPE_ENEMY_SINGLE;
        }
        else if (action.equals("defense")) {
            instruct = INSTRUCT_DEFENSE;
            targetType = TARGET_TYPE_SELF;
        }
        else if (action.startsWith("cast:")) {
            instruct = INSTRUCT_CAST;
            String code = action.substring(5);
            skill = src.getSkillByCode(code);
            if (skill != null) {
                targetType = skill.getTargetType();
            }
            else {
                setAction("attack");
            }
        }
        else if (action.startsWith("item:")) {
            instruct = INSTRUCT_ITEM;
            String code = action.substring(5);
            item = src.getItemByCode(code);
            if (item != null) {
                targetType = item.getTargetType();
            }
            else {
                setAction("attack");
            }
        }
        else if (action.equals("run")) {
            instruct = INSTRUCT_RUN;
        }
        else {
            instruct = INSTRUCT_WAIT;
        }
    }

    @Override
    public int compareTo(Command o) {
        if (speed>o.speed) return -1;
        else if (speed<o.speed) return 1;
        else return 0;
    }

    @Override
    public String toString() {
        String command = null;
        switch (instruct) {
            case INSTRUCT_ATTACK:
                // Unit target = targets.get(0);
                command = src.getName() + "发动攻击";
                break;
            case INSTRUCT_DEFENSE:
                command = src.getName() + "正在防御";
                break;
            case INSTRUCT_CAST:
                command = src.getName() + skill.getActionDescription();
                break;
            case INSTRUCT_ITEM:
                command = src.getName() + item.getActionDescription();
                break;
            case INSTRUCT_WAIT:
                command = src.name + "什么也没做";
        }
        return command;
    }

    // --------------------

    public Unit getSrc() {
        return src;
    }

    public void setSrc(Unit src) {
        this.src = src;
    }

    public List<Unit> getTargets() {
        return targets;
    }

    public void setTargets(List<Unit> targets) {
        this.targets = targets;
    }

    public String getAction() {
        return action;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getInstruct() {
        return instruct;
    }

    public void setInstruct(int instruct) {
        this.instruct = instruct;
    }

    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public int getTargetType() {
        return targetType;
    }
}
